<?php

namespace App\Models\Article;

use App\Models\BaseModel;
use App\Models\Kms\User\KmsUser;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class ArticleModel extends BaseModel
{
    use HasFactory, KfUid, KfSoftDeletes;

    protected $table = "article";

    protected $fillable = [
        "content",
        "transcodingVideoDetail"
    ];

    const TYPE_ARTICLE = 1;
    const TYPE_VIDEO   = 2;
    const TYPE_TEXT    = [
        self::TYPE_VIDEO   => "视频",
        self::TYPE_ARTICLE => "文章",
    ];

    const STATUS_ON   = 1;
    const STATUS_OFF  = 0;
    const STATUS_TEXT = [
        self::STATUS_OFF => "下架",
        self::STATUS_ON  => "上架",
    ];

    const PUBLISH_SELECTION_YES = 1;
    const PUBLISH_SELECTION_NO  = 0;
    /**
     * 列表下拉选项[ 发布状态 ]
     */
    const PUBLISH_SELECTION_TYPE_TEXT = [
        self::PUBLISH_SELECTION_YES => "已发布",
        self::PUBLISH_SELECTION_NO  => "待发布",
    ];

    const PUT_TOP_YES  = 1;
    const PUT_TOP_NO   = 0;
    const PUT_TOP_TEXT = [
        self::PUT_TOP_NO  => "常规",
        self::PUT_TOP_YES => "置顶",
    ];


    const PUBLISH_TYPE_DRAFT  = 0;
    const PUBLISH_TYPE_TIMING = 1;
    const PUBLISH_TYPE_NOW    = 2;
    const PUBLISH_TYPE_TEXT   = [
        self::PUBLISH_TYPE_DRAFT  => "保存草稿",
        self::PUBLISH_TYPE_TIMING => "定时发布",
        self::PUBLISH_TYPE_NOW    => "立即发布",
    ];

    const PUBLISH_NO_DRAFT_TYPE_TEXT = [
        self::PUBLISH_TYPE_TIMING => "定时发布",
        self::PUBLISH_TYPE_NOW    => "立即发布",
    ];


    const INDEX_TAB_TYPE_0 = 0;
    const INDEX_TAB_TYPE_1 = 1;
    const INDEX_TAB_TYPE_2 = 2;
    const INDEX_TAB_TYPE_3 = 3;
    const INDEX_TAB_TYPE_TEXT = [
        self::INDEX_TAB_TYPE_0 => "信息流",
        self::INDEX_TAB_TYPE_1 => "最新",
        self::INDEX_TAB_TYPE_2 => "热榜",
        self::INDEX_TAB_TYPE_3 => "专题",
    ];

    /**
     * @return HasOne
     */
    public function createUserRel(): HasOne
    {
        return $this->hasOne(KmsUser::class, "rmsUserId", "createdByUid");
    }

    public static function getCount(): int
    {
        return self::query()
//            ->where('status', ArticleModel::STATUS_ON)
                   ->whereIn("publishType", array_keys(ArticleModel::PUBLISH_NO_DRAFT_TYPE_TEXT))
//            ->where(
//                function ($query) {
//                    $query->where("effectiveTime", "<=", time());
//                }
//            )
                   ->count();
    }

    /**
     * @return HasMany
     */
    public function articleUserRelationRel(): HasMany
    {
        return $this->HasMany(ArticleUserRelationModel::class, "articleId", "id");
    }

    /**
     * @return HasMany
     */
    public function articleCategoryMappingsRel(): HasMany
    {
        return $this->hasMany(ArticleCategoryMappingsModel::class, "articleId", "id");
    }
}
